package com.ccl.iot.huiju.db;

import com.ccl.iot.App;
import com.ccl.iot.bean.IOTHuiJuBean;
import com.ccl.iot.bean.IOTHuiJuBeanDao;

import java.util.List;

/**
 * 类说明：
 * 作者：HD on 2017/10/24 16:29
 * 邮箱：hehandong@qq.com
 */

public class IOTDaoUtil {

    public static List<IOTHuiJuBean> queryAllData() {
        IOTHuiJuBeanDao iotHuiJuBeanDao = App.getIOTHuiJuBeanDao();
        if (iotHuiJuBeanDao != null) {
            return iotHuiJuBeanDao.queryBuilder().list();
        }
        return null;
    }

    public static List<IOTHuiJuBean> queryDataFromIOTId(String iotId) {
        IOTHuiJuBeanDao iotHuiJuBeanDao = App.getIOTHuiJuBeanDao();
        if (iotHuiJuBeanDao != null) {
            return iotHuiJuBeanDao.queryBuilder().where(IOTHuiJuBeanDao.Properties.IOTId.eq(iotId)).list();
        }
        return null;
    }

    public static List<IOTHuiJuBean> queryDataFromPage(int pageIndex) {
        IOTHuiJuBeanDao iotHuiJuBeanDao = App.getIOTHuiJuBeanDao();
        if (iotHuiJuBeanDao != null) {
            return iotHuiJuBeanDao.queryBuilder().where(IOTHuiJuBeanDao.Properties.PageIndex.eq(pageIndex)).list();
        }
        return null;
    }

    public static IOTHuiJuBean queryDataFromPagePosition(int pageIndex, int positionIndex) {
        IOTHuiJuBeanDao iotHuiJuBeanDao = App.getIOTHuiJuBeanDao();
        if (iotHuiJuBeanDao != null) {
            List<IOTHuiJuBean> huiJuBeanList = iotHuiJuBeanDao.queryBuilder().
                    where(IOTHuiJuBeanDao.Properties.PageIndex.eq(pageIndex),
                            IOTHuiJuBeanDao.Properties.PositionIndex.eq(positionIndex)).list();

            if (huiJuBeanList != null && huiJuBeanList.size() > 0) {
                return huiJuBeanList.get(0);
            }
        }
        return null;
    }


    public static List<IOTHuiJuBean> queryDataListFromPagePosition(int pageIndex, int positionIndex) {
        IOTHuiJuBeanDao iotHuiJuBeanDao = App.getIOTHuiJuBeanDao();
        if (iotHuiJuBeanDao != null) {
            return iotHuiJuBeanDao.queryBuilder().where(IOTHuiJuBeanDao.Properties.PageIndex.eq(pageIndex),
                    IOTHuiJuBeanDao.Properties.PositionIndex.eq(positionIndex)).list();

        }
        return null;
    }

    public static int queryDataPage(String iotId) {
        IOTHuiJuBean iotHuiJuBean = queryData(iotId);
        if (iotHuiJuBean != null) {
            return iotHuiJuBean.getPageIndex();
        }
        return -1;
    }

    public static IOTHuiJuBean queryData(String iotId) {

        List<IOTHuiJuBean> list = queryDataFromIOTId(iotId);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }


    public static void saveData(String iotId, String name, String type, long devId, int pageIndex, int positionIndex) {

        IOTHuiJuBean iotHuiJuBean = queryData(iotId);
        if (iotHuiJuBean != null) {
            iotHuiJuBean.setPageIndex(pageIndex);
            iotHuiJuBean.setPositionIndex(positionIndex);
            App.getIOTHuiJuBeanDao().update(iotHuiJuBean);
        } else {
            iotHuiJuBean = new IOTHuiJuBean();
            iotHuiJuBean.setIOTId(iotId);
            iotHuiJuBean.setName(name);
            iotHuiJuBean.setType(type);
            iotHuiJuBean.setDevId(devId);
            iotHuiJuBean.setPageIndex(pageIndex);
            iotHuiJuBean.setPositionIndex(positionIndex);
            App.getIOTHuiJuBeanDao().insert(iotHuiJuBean);
        }
    }

    public static void savePositionData(String iotId, String name, String type, long devId, int pageIndex, int positionIndex) {

        //重置旧位置数据
        List<IOTHuiJuBean> huiJuBeanList = queryDataListFromPagePosition(pageIndex, positionIndex);

        if (huiJuBeanList != null && huiJuBeanList.size() > 0) {
            int size = huiJuBeanList.size();
            for (int i = 0; i < size; i++) {
                IOTHuiJuBean iotHuiJu = huiJuBeanList.get(i);
                if (iotHuiJu != null) {
                    iotHuiJu.setPageIndex(-1);
                    iotHuiJu.setPositionIndex(-1);
                    App.getIOTHuiJuBeanDao().update(iotHuiJu);
                }
            }
        }

        saveData(iotId, name, type, devId, pageIndex, positionIndex);

    }

    public static void deletePositionData(int pageIndex, int positionIndex) {

        //重置旧位置数据
        List<IOTHuiJuBean> huiJuBeanList = queryDataListFromPagePosition(pageIndex, positionIndex);

        if (huiJuBeanList != null && huiJuBeanList.size() > 0) {
            int size = huiJuBeanList.size();
            for (int i = 0; i < size; i++) {
                IOTHuiJuBean iotHuiJu = huiJuBeanList.get(i);
                if (iotHuiJu != null) {
                    iotHuiJu.setPageIndex(-1);
                    iotHuiJu.setPositionIndex(-1);
                    App.getIOTHuiJuBeanDao().update(iotHuiJu);
                }
            }
        }


    }


}
